﻿using Ebusiness_Authservice.Entities;
using Microsoft.EntityFrameworkCore;
using OpenIddict.EntityFrameworkCore.Models;
using System.Reflection.Emit;

//using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
//using Volo.Abp.Identity.EntityFrameworkCore;
//using Volo.Abp.Identity;
//using Volo.Abp.OpenIddict;
//using Volo.Abp.OpenIddict.EntityFrameworkCore;
//using Volo.Abp.PermissionManagement.EntityFrameworkCore;
//using Volo.Abp.PermissionManagement;

namespace Ebusiness_Authservice.Data;

public class Ebusiness_AuthserviceDbContext : AbpDbContext<Ebusiness_AuthserviceDbContext>
{

    public DbSet<OpenIddictEntityFrameworkCoreApplication> Applications { get; set; }
    public DbSet<OpenIddictEntityFrameworkCoreAuthorization> Authorizations { get; set; }
    public DbSet<OpenIddictEntityFrameworkCoreScope> Scopes { get; set; }
    public DbSet<OpenIddictEntityFrameworkCoreToken> Tokens { get; set; }

    public DbSet<ApplicationUser>  applicationUsers { get; set; }

    public Ebusiness_AuthserviceDbContext(DbContextOptions<Ebusiness_AuthserviceDbContext> options)
        : base(options)
    {

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="builder"></param>
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<OpenIddictEntityFrameworkCoreApplication>().ToTable("Yuan_OpenIddictApplications");
        builder.Entity<OpenIddictEntityFrameworkCoreAuthorization>().ToTable("Yuan_OpenIddictAuthorizations");
        builder.Entity<OpenIddictEntityFrameworkCoreScope>().ToTable("Yuan_OpenIddictScopes");
        builder.Entity<OpenIddictEntityFrameworkCoreToken>().ToTable("Yuan_OpenIddictTokens");

        // 配置用户表结构
        builder.Entity<ApplicationUser>(entity =>
        {
            entity.ToTable("Yuan_User");
            entity.HasKey(e => e.Id);  // 设置主键
            entity.Property(e => e.UserName).IsRequired().HasMaxLength(256); // 用户名必填，最大长度256
            entity.Property(e => e.Email).IsRequired().HasMaxLength(256);    // 邮箱必填，最大长度256
            entity.HasIndex(e => e.UserName).IsUnique(); // 用户名唯一索引
            entity.HasIndex(e => e.Email).IsUnique();    // 邮箱唯一索引
        });

        /* Include modules to your migration db context */
        //builder.ConfigureAuditLogging();
        /* Configure your own entities here */

        //分布式认证系统
        //AbpOpenIddictDbProperties.DbTablePrefix = "YDT_" + AbpOpenIddictDbProperties.DbTablePrefix;
        //builder.ConfigureOpenIddict();

        ////AbpIdentityDbProperties.DbTablePrefix = ""; // 2、去掉Identity表前缀
        //builder.ConfigureIdentity(); // 使用Identity

        ////AbpPermissionManagementDbProperties.DbTablePrefix = ""; // 3、去掉PermissionManagement表前缀
        //builder.ConfigurePermissionManagement(); // 使用PermissionManagement
    }
}
